#include<PaleGem.h>

std::list<IObject*> pgObject::createdInstances;
std::list<IObject*> pgObject::deletedInstances;
int pgObject::numberOfInstances = 0;


pgObject::pgObject()
{
	
}

pgObject::~pgObject()
{

}

void pgObject::AddRef()
{
	
}

void pgObject::Release()
{
	referenceCount--;
	if (referenceCount <= 0)
	{
		createdInstances.remove(this);
		deletedInstances.push_back(this);
	}
}

void pgObject::CollectGarbage()
{
	std::list<IObject*>::iterator itDel = deletedInstances.begin();

	while (itDel != deletedInstances.end())
	{
		delete(*itDel);
		itDel++;
	}

	deletedInstances.clear();
}

bool pgObject::ClearAll()
{
	std::list<IObject*>::iterator it = createdInstances.begin();

	CollectGarbage();
	if (createdInstances.size() != 0)
	{
		for (it = createdInstances.begin(); it != createdInstances.end(); ++it)
		{
			IObject *o = (*it);
			if (o->id != g_pGameApp->id)
			{
				PG_LOG_ERROR("Not all objects have been released! Cleaning now!", -666, 0,0);
				delete(o);
			}
		}
	}
	createdInstances.clear();
	return true;
}

